using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.SmartParts;
using Jbe.CABExtension.WinForms;
using Jbe.TestSuite.LogViewer.Properties;
using Jbe.TestSuite.Infrastructure.Interface.Constants;
using Jbe.CABExtension.WinForms.Workspaces;
using Jbe.CABExtension.SmartPartInfos;
using System.Reflection;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.LogViewer
{
    public class ModuleController : WorkItemController
    {
        private LogView logView;
        private IUIElementCreationService uiElementCreationService;

        [ServiceDependency]
        public IUIElementCreationService UIElementCreationService
        {
            set { uiElementCreationService = value; }
        }

        public override void Run()
        {
            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(
                "Log Viewer", Resources.EventLog.ToBitmap());
            WorkItem.UIExtensionSites[UIExtensionSiteNames.ViewMenuItem].Add(menuItem);
            menuItem.Click += new EventHandler(LogViewerMenuItemClick);

            ShowLogViewer();
        }

        private void ShowLogViewer()
        {
            if (logView == null)
            {
                DockPanelSmartPartInfo dpInfo = new DockPanelSmartPartInfo("Log Viewer", "Log Viewer");
                dpInfo.Icon = Resources.EventLog;
                dpInfo.DockingType = DockingType.TaskView;
                dpInfo.ShowStrategy = new DockStateShowStrategy(DockState.DockBottom);

                logView = WorkItem.SmartParts.AddNew<LogView>();
                logView.Disposed += new EventHandler(LogViewDisposed);
                WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(logView, dpInfo);
            }
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Activate(logView);
        }

        #region Command and Event handler

        private void LogViewerMenuItemClick(object sender, EventArgs e)
        {
            ShowLogViewer();
        }

        private void LogViewDisposed(object sender, EventArgs e)
        {
            WorkItem.SmartParts.Remove(logView);
            logView = null;
        }

        #endregion
    }
}
